/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa.session;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import jpa.entities.Usuario;

/**
 *
 * @author carlos
 */
@Stateless
public class UsuarioFacade extends AbstractFacade<Usuario> {

    @PersistenceContext(unitName = "LaCruz_v2PU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public UsuarioFacade() {
        super(Usuario.class);
    }

     public Usuario findUsuarioLogado(String username) {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Usuario> query = builder.createQuery(Usuario.class);
        Root<Usuario> Usuario = query.from(Usuario.class);

        List<Predicate> predicateList = new ArrayList<Predicate>();

        predicateList.add( builder.equal(Usuario.get("email"), username));

        Predicate[] predicates = new Predicate[predicateList.size()];
        query.where(predicateList.toArray(predicates));
        query.select(Usuario);

        return getEntityManager().createQuery(query).getSingleResult();


    }
    public Usuario findFilters(String username, String password) {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Usuario> query = builder.createQuery(Usuario.class);
        Root<Usuario> Usuario = query.from(Usuario.class);

        List<Predicate> predicateList = new ArrayList<Predicate>();

        predicateList.add( builder.equal(Usuario.get("email"), username));

        predicateList.add(builder.equal(Usuario.get("senha"), password));

        Predicate[] predicates = new Predicate[predicateList.size()];
        query.where(predicateList.toArray(predicates));
        query.select(Usuario);

        return getEntityManager().createQuery(query).getSingleResult();


    }
}
